﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace Pyramid_Game
{
    public abstract class RouteFind : GameScreen
    {
        protected Texture2D backGroundTexture;
        protected Texture2D routeTexture;
        protected Texture2D notRouteTexture;
        protected Texture2D hitRouteTexture;

        protected Space[,] space = new Space[4, 10];
        protected List<Space> StorageRoute = new List<Space>();
        protected Space currentSpace;

        protected int size = 80;
        protected int width = 10;
        protected int height = 4;

        protected bool start = false;
        protected bool end = false;

        public RouteFind()
        {

        }

        public override void LoadContent(ContentManager content)
        {
            backGroundTexture = content.Load<Texture2D>("Texture/blank");
            routeTexture = content.Load<Texture2D>("Texture/route/route");
            notRouteTexture = content.Load<Texture2D>("Texture/route/notroute");
            hitRouteTexture = content.Load<Texture2D>("Texture/route/routehit");
        }

        public override void Update(Touch touch, GameTime gameTime)
        {
            if (end)
            {
                //수정해야함
                //screenManager.RemoveScreen(this);
                //screenManager.AddScreen(new resultScreen(0, "FlyBat"));
            }

            HandleInput(touch, gameTime);
            CheckEnd();
        }
        public void HandleInput(Touch touch, GameTime gameTime)
        {
            foreach (GestureSample gesture in touch.Gestures)
            {
                Point tapLocation = new Point((int)gesture.Position.X, (int)gesture.Position.Y);

                if (gesture.GestureType == GestureType.Tap)
                {
                    foreach (Space s in space)
                    {
                        if (tapLocation.X > s.Position.X &&
                            tapLocation.X < s.Position.X + s.SpaceTexture.Width &&
                            tapLocation.Y > s.Position.Y &&
                            tapLocation.Y < s.Position.Y + s.SpaceTexture.Height)
                        {
                            if (!start)
                            {
                                AddSpace(s);
                                UpdateCurrentSpace();
                                s.HitSpace(hitRouteTexture);
                                start = true;
                                return;
                            }

                            if (s.IsHit)
                            {
                                if (s == currentSpace)
                                {
                                    s.ReturnSpace(routeTexture);
                                    RemoveSpace();
                                }
                            }
                            else
                            {
                                if (!s.IsRoute)
                                {
                                    return;
                                }
                                foreach (Space sc in currentSpace.PossibleRoute)
                                {
                                    if (sc == s)
                                    {
                                        s.HitSpace(hitRouteTexture);
                                        AddSpace(s);
                                    }
                                }
                            }

                            UpdateCurrentSpace();
                        }
                    }
                }
            }
        }

        public override void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            spriteBatch.Draw(backGroundTexture, Vector2.Zero, Color.White);

            foreach (Space s in space)
            {
                spriteBatch.Draw(s.SpaceTexture, s.Position, Color.White);
            }
        }

        public void AddPossibleRoute()
        {
            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    int k = j - 1;
                    k = (int)MathHelper.Clamp(k, 0, width-1);
                    space[i, j].AddPossibleRoute(space[i, k]);

                    k = j + 1;
                    k = (int)MathHelper.Clamp(k, 0, width-1);
                    space[i, j].AddPossibleRoute(space[i, k]);

                    k = i - 1;
                    k = (int)MathHelper.Clamp(k, 0, height-1);
                    space[i, j].AddPossibleRoute(space[k, j]);

                    k = i + 1;
                    k = (int)MathHelper.Clamp(k, 0, height-1);
                    space[i, j].AddPossibleRoute(space[k, j]);
                }
            }
        }

        public void RemoveSpace()
        {
            StorageRoute.RemoveAt(StorageRoute.Count() - 1);
        }
        public void AddSpace(Space space)
        {
            StorageRoute.Add(space);
        }
        public void UpdateCurrentSpace()
        {
            if (StorageRoute.Count() == 0)
            {
                start = false;
            }
            else
                currentSpace = StorageRoute[StorageRoute.Count() - 1];
        }
        public void CheckEnd()
        {
            int count = 0;

            foreach (Space s in space)
            {
                if (s.IsRoute && !s.IsHit)
                    count++;
            }

            if (count == 0)
                end = true;
        }
    }
}
